var {
  timeUtils,
  appUtils,
  randomUtils,
  textUtils,
  logUtils,
} = require("../../utils/index");
var Action = require("../../base/Action");
var aRead2Hour = new Action("阅读", {});
aRead2Hour.exec = function () {
  var baseSpan = 140;
  var span = randomUtils.getRandom(baseSpan, baseSpan + 5, 1);
  logUtils.log("本轮将运行", span, "分钟");
  var startTime = Date.now();
  while (true) {
    this.read();
    var hasExec = timeUtils.hasExec(startTime);
    logUtils.log("本轮已运行", span * 60, "/", hasExec);
    if (hasExec > span * 60) {
      logUtils.log("关闭啦啦啦啦！！！");
      appUtils.closeRecent();
      sleep(randomUtils.getRandom(2, 5, 1000));
      lockScreen();
      break;
    }
  }
};
aRead2Hour.read = function () {
  var baseSpan = 20;
  var span = randomUtils.getRandom(baseSpan, baseSpan + 100);
  logUtils.log("本次将运行", span, "秒");
  var startTime = Date.now();
  while (true) {
    sleep(5 * 1000);
    if (this.check()) {
      span = span + 5;
    }
    var hasExec = timeUtils.hasExec(startTime);
    logUtils.log("本次已运行", span, "/", hasExec);
    if (hasExec > span) {
      break;
    }
  }
  home();
  sleep(randomUtils.getRandom(2, 8, 1000));
  appUtils.open("百度极速版");
};
aRead2Hour.check = function () {
  var content = "确认购买";
  if (textUtils.existsTextStartsWith(content)) {
    back();
    sleep(randomUtils.getRandom(2, 5, 1000));
    var content = "放弃优惠";
    if (textUtils.clickTextStartsWith(content)) {
      sleep(randomUtils.getRandom(2, 5, 1000));
    }
    back();
    sleep(randomUtils.getRandom(2, 5, 1000));
    return true;
  }
  var content = "下滑";
  if (textUtils.existsTextStartsWith(content)) {
    back();
    sleep(randomUtils.getRandom(2, 5, 1000));
    return true;
  }
  return false;
};
module.exports = aRead2Hour;
